package com.cjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjh.pojo.Emp;
import com.cjh.pojo.EmpSearch;
import com.cjh.pojo.ResultMap;
import com.cjh.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/${spring.application.name}/api")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/selectList")
    public ResultMap linkedSelect(@RequestParam(value = "page",defaultValue = "1",required = false) Integer page
            , @RequestParam(value = "limit",defaultValue = "10",required = false) Integer limit){

        Page<Emp> p=new Page<>(page,limit);
        List<Emp> emps = empService.linkedSelect(p);
        return new ResultMap<List<Emp>>(0,"关联查询所有员工",emps, (int) p.getTotal());
    }

    @GetMapping("/selectById")
    public ResultMap selectById(@RequestParam(value = "id", required = false) Integer id){

        Emp emp = empService.selectById(id);
        return new ResultMap<Emp>(200,"根据id查询员工",emp);
    }

    @PostMapping("/search")
    public ResultMap search(EmpSearch empSearch){

        List<Emp> emps = empService.searchEmp(empSearch);
        return new ResultMap<List<Emp>>(200,"根据多条件查询员工",emps,emps.size());
    }

    @PostMapping("/insert")
    public ResultMap insert(Emp emp){

        Emp insertEmp = empService.insert(emp);
        return new ResultMap<String>(200,"新增一个员工","新增成功",1);
    }

    @DeleteMapping("/deleteOneOrBatch")
    public ResultMap deleteOneOrBatch(@RequestParam(value = "id", required = false) List id){

        Integer count = empService.deleteOneOrBatch(id);
        return new ResultMap<String>(200,"根据多个id删除员工","删除成功",count);
    }

    @PutMapping("/updateById")
    public ResultMap updateById(Emp emp){

        Emp updateEmp = empService.updateById(emp);
        return new ResultMap<String>(200,"根据id修改一个员工","修改成功",1);
    }
}
